package com.security.system.controller;

import com.security.Utils.CaptchaUtils;
import com.security.system.dao.SysUserDao;
import com.security.system.entity.MobileCode;
import com.security.system.entity.SysUser;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 手机 验证码 接口
 */
@Slf4j
@Controller
@RequestMapping("mobileCapcha")
public class MobileCapchaController {

    @Autowired
    SysUserDao dao;

    //获取 并 发送 手机验证码
    @PostMapping("getMobileCapcha")
    @ResponseBody
    public Map<String,Object> getMobileCapcha(@RequestBody Map<String,Object> param, HttpSession session){
        Map<String,Object> res = new HashMap<>();
        SysUser user =  dao.findSysUserByMobile(param);
        if(user == null){
            res.put("code",2001);
            res.put("msg","您输入的手机号未曾注册");
            return res;
        }
        MobileCode smsCode = new MobileCode(CaptchaUtils.MOBILE_Captcha().getCode(),user.getMobile(),60);
        session.setAttribute(CaptchaUtils.CAPTCHA_MOBILE_KEY,smsCode);
        log.info("短信已发出"+smsCode.getCode());
        res.put("code",200);
        res.put("mobilecode",smsCode.getCode());
        res.put("msg","ok");
        return res;
    }
}
